package com.example.mobile2032021606168xmy0602homework;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private EditText edtTxtMainUsernumber;
    private EditText edtTxtMainUserpassword;
    private CheckBox chkBoxMainRemberpassword;
    private CheckBox chkBoxMainAutoland;
    private Button btnMainLogin;
    private Button btnMainLogon;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        sp = getSharedPreferences("login",MODE_PRIVATE);
        chkBoxMainRemberpassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean b) {
                SharedPreferences.Editor editor=sp.edit();
                editor.putBoolean("chkName",b);
                editor.commit();

            }
        });
        chkBoxMainAutoland.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean b) {
                SharedPreferences.Editor editor= sp.edit();
                editor.putBoolean("chkPSW",b);
                editor.commit();
                if (b){
                    chkBoxMainRemberpassword.setChecked(true);
                    chkBoxMainRemberpassword.setEnabled(false);
                }else {
                    chkBoxMainRemberpassword.setEnabled(true);
                }

            }
        });
        btnMainLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,ShowDataActivity.class);
                intent.putExtra("userName",edtTxtMainUsernumber.getText().toString().trim());
                Bundle bundle = new Bundle();
                bundle.putString("passWord",edtTxtMainUserpassword.getText().toString().trim());
                intent.putExtras(bundle);
                startActivity(intent);




                String userName = edtTxtMainUsernumber.getText().toString();
                String password = edtTxtMainUserpassword.getText().toString();
                if (userName.equals("admin")&&password.equals("admin")){
                    if (chkBoxMainRemberpassword.isChecked()){
                        SharedPreferences.Editor editor =sp.edit();
                        editor.putString("userName",userName);
                        editor.commit();
                    }
                    if (chkBoxMainAutoland.isChecked()){
                        SharedPreferences.Editor editor=sp.edit();
                        editor.putString("password",password);
                        editor.commit();
                    }
                }
            }
        });
        btnMainLogon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("密码或用户名输入错误")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();

                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        boolean chkName = sp.getBoolean("chkName",false);
        chkBoxMainRemberpassword.setChecked(chkName);
        boolean chkPSW =sp.getBoolean("chkPSW",false);
        chkBoxMainAutoland.setChecked(chkPSW);
        if (chkName){
            edtTxtMainUsernumber.setText(sp.getString("userName",null));
        }
        if (chkPSW){
            edtTxtMainUserpassword.setText(sp.getString("password",null));
        }
    }



    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        edtTxtMainUsernumber = (EditText) findViewById(R.id.edtTxt_main_usernumber);
        edtTxtMainUserpassword = (EditText) findViewById(R.id.edtTxt_main_userpassword);
        chkBoxMainRemberpassword = (CheckBox) findViewById(R.id.chkBox_main_remberpassword);
        chkBoxMainAutoland = (CheckBox) findViewById(R.id.chkBox_main_autoland);
        btnMainLogin = (Button) findViewById(R.id.btn_main_login);
        btnMainLogon = (Button) findViewById(R.id.btn_main_logon);
    }
}